using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace Edmx.Storage
{
    [DebuggerDisplay("Name={Name}, Parameters={Parameters.Count}")]
    [XmlType(Namespace = EdmxConstants.StorageSchemaNamespace)]
    public class Function : MetadataEntityBase<Function>
    {
        public Function()
        {
            IsComposable = true;
            ParameterTypeSemantics = ParameterTypeSemantics.AllowImplicitConversion;
        }

        [XmlElement("CommandText")]
        public string CommandText { get; set; }

        [XmlElement("Parameter")]
        public ParameterCollection Parameters { get; set; }

        [XmlAttribute("Name")]
        public string Name { get; set; }

        [XmlAttribute("ReturnType")]
        public string ReturnType { get; set; }

        private bool? _aggregate;

        [XmlAttribute("Aggregate")]
        public bool Aggregate
        {
            get { return _aggregate ?? false; }
            set { _aggregate = value; }
        }

        public bool ShouldSerializeAggregate()
        {
            return _aggregate.HasValue;
        }

        private bool? _builtIn;

        [XmlAttribute("BuiltIn")]
        public bool BuiltIn
        {
            get { return _builtIn ?? false; }
            set { _builtIn = value; }
        }

        public bool ShouldSerializeBuiltIn()
        {
            return _builtIn.HasValue;
        }

        [XmlAttribute("StoreFunctionName")]
        public string StoreFunctionName { get; set; }

        private bool? _niladicFunction;

        [XmlAttribute("NiladicFunction")]
        public bool NiladicFunction
        {
            get { return _niladicFunction ?? false; }
            set { _niladicFunction = value; }
        }

        public bool ShouldSerializeNiladicFunction()
        {
            return _niladicFunction.HasValue;
        }

        [XmlAttribute("IsComposable")]
        [DefaultValue(true)]
        public bool IsComposable { get; set; }

        private ParameterTypeSemantics? _parameterTypeSemantics;

        [XmlAttribute("ParameterTypeSemantics")]
        public ParameterTypeSemantics ParameterTypeSemantics
        {
            get { return _parameterTypeSemantics ?? ParameterTypeSemantics.AllowImplicitConversion; }
            set { _parameterTypeSemantics = value; }
        }

        public bool ShouldSerializeParameterTypeSemantics()
        {
            return _parameterTypeSemantics.HasValue;
        }

        [XmlAttribute("Schema")]
        public string Schema { get; set; }

        [XmlAttribute("Name", Form = XmlSchemaForm.Qualified, Namespace = EdmxConstants.StoreGeneratorNamespace)]
        public string StoreName { get; set; }

        [XmlAttribute("Schema", Form = XmlSchemaForm.Qualified, Namespace = EdmxConstants.StoreGeneratorNamespace)]
        public string StoreSchema { get; set; }
    }
}